package csc309.datingapp;

import java.util.Map;
import java.sql.*;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/*
 * The Login action takes a username and password from the request
 * and authenticates those tokens agains the portfolioService objects
 * authentication methods.  If the user is valid, the user object
 * is stored in the session map. 
 * 
 * To receive the session map, this action implements the sessionAware
 * interface.  This decouples the action from the servlet API.  Of course,
 * the action could use the ActionContext to get this map, or even the 
 * underlying Servlet Session object, but these would be needless bindings 
 * to the Servlet API, certainly complicating testing. 
 */

public class Login extends ActionSupport implements SessionAware {
	public String errormsg;

	public String getErrormsg() {
		return errormsg;
	}

	public void setErrormsg(String error) {
		this.errormsg = error;
	}

	public String execute() {

		Connection con;
		try {
			
			String dbUsername = "group09";
			String dbPassword = "asdasdqr3ggqq";

			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://bungle07.cs.toronto.edu", dbUsername,
					dbPassword);

			Statement sql = con.createStatement();

			setUsername(getUsername());
			setPassword(getPassword());

		ResultSet results = sql.executeQuery("select * from group09.users where email='"
							+ getUsername() + "' AND password='" + getPassword() + "'");
			
			//ResultSet results = sql.executeQuery("select * from group09.users where email='pimp' and password='asd'");
		
			if (results != null) {
				while (results.next()) {
					if(results.getString("email").equals(getUsername()) && results.getString("password").equals(getPassword())) {
						session.put("username", getUsername());
						session.put("loggedIn", "true");
						
						results.close();
						sql.close();
						con.close();
						return "success";
					}
				}
			}
 
			sql.close();
			con.close();
			return "exception";
		} catch (Exception e) {
			setErrormsg(e.getMessage());
			return "exception";
		}		
	}

	/* JavaBeans Properties to Receive Request Parameters */

	private String username;
	private String password;
	private Map<String, String> session;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void validate() {

//		/* Check that fields are not empty */
//		if (getPassword().length() == 0) {
//			addFieldError("password", getText("password.required"));
//		}
//		if (getUsername().length() == 0) {
//			addFieldError("username", getText("username.required"));
//		}

	}

	public void setSession(Map session) {
		this.session = session;

	}

}
